home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / synthia / kbd_tex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.5 KB  |  156 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  * Copyright (C) 1991, Silicon Graphics, Inc.
  19.  * All Rights Reserved.
  20.  */
  21.     /*
  22.      *  Define textures used in keyboard display
  23.      *
  24.      *  Routines:
  25.      *    def_tex ()
  26.      *    file_to_tex (fname, tex_id)
  27.      *
  28.      *  Jim Bennett
  29.      *  1991
  30.      */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <gl/image.h>
  35. #include <gl/gl.h>
  36. #include "paths.h"
  37. #include "globj.h"
  38.  
  39. int    valid_texture;
  40.  
  41. #define    NTEV    2
  42. float    decal_tev [NTEV] = { TV_DECAL, TV_NULL };
  43.  
  44. #define    IMAGE_NAME    "logo.rgb"
  45.  
  46.     /*
  47.      *  def_tex  -  Define keyboard textures
  48.      */
  49.  
  50. def_tex ()
  51.  
  52.     {
  53.  
  54. /* If texture mapping not supported, punt on this.    */
  55.  
  56.     valid_texture = FALSE;
  57.     if (!getgdesc (GD_TEXTURE)) return;
  58.  
  59. /* Load the two textures, and define the texture environment    */
  60.  
  61.     if (!file_to_tex ("logo.rgb", LOGO_TEX))
  62.         return;
  63.     if (!file_to_tex ("title.rgb", TITLE_TEX))
  64.         return;
  65.  
  66. /* All OK, set the texture valid flag and exit    */
  67.  
  68.     tevdef (DECAL_ENV, NTEV, decal_tev);
  69.     valid_texture = TRUE;
  70.     }
  71.  
  72.     /*
  73.      *  file_to_tex  -  Convert image file to texture
  74.      *
  75.      *    Returns TRUE if conversion succeeded
  76.      */
  77.  
  78. file_to_tex (fname, tex_id)
  79.  
  80.     char    *fname;
  81.     int    tex_id;
  82.  
  83.     {
  84.     IMAGE    *ifile;
  85.     char    *namebuf, *path;
  86.     int    width, height, depth;
  87.     unsigned long *teximage;
  88.     short    *buf;
  89.     int    i, j, k;
  90.     unsigned char *txp;
  91.     int    luminance;
  92.  
  93. /* Make sure a valid texture image is available.    */
  94.  
  95.     if (!(path = getenv("SYNTHIA_IMAGE_PATH")))
  96.         path = IMAGE_PATH;
  97.     namebuf = (char *)malloc (strlen(fname) + strlen(path) + 2);
  98.     strcpy (namebuf, path);
  99.     strcat (namebuf, "/");
  100.     strcat (namebuf, fname);
  101.     ifile = iopen (namebuf, "r");
  102.     if (ifile == NULL) return (FALSE);
  103.  
  104. /* Get the size of the image, and allocate a texture buffer for it    */
  105.  
  106.     width = ifile->xsize;
  107.     height = ifile->ysize;
  108.     depth = ifile->dim;
  109.  
  110.     if (depth != 3) return (FALSE);        /* RGB file required.    */
  111.  
  112.     teximage = (unsigned long *)malloc (height * width * sizeof (long));
  113.     if (teximage == NULL) return (FALSE);
  114.  
  115.     bzero (teximage, height * width * sizeof (long));
  116.  
  117. /* Read the image into the texture buffer    */
  118.  
  119.     buf = (short *)malloc (width * sizeof (short));
  120.     if (buf == NULL) return (FALSE);
  121.  
  122.     for (i=0; i<height; i++)
  123.         {
  124.         for (j=0; j<depth; j++)
  125.             {
  126.             txp = (unsigned char *)teximage +
  127.                 (i * (width * sizeof (long))) +
  128.                 (depth - j);
  129.             getrow (ifile, buf, i, j);
  130.             for (k=0; k<width; k++, txp += 4)
  131.                 *txp = buf[k];
  132.             }
  133.  
  134.     /* Fill in the alpha channel = 1.0 where image is above zero    */
  135.  
  136.         txp = (unsigned char *)teximage + (i * (width * sizeof (long)));
  137.         for (k=0; k<width; k++, txp += 4)
  138.             {
  139.             luminance = *(txp+1) + *(txp+2) + *(txp+3);
  140.             if (luminance > 25)
  141.                 *txp = 255;
  142.             }
  143.         }
  144.  
  145. /* Finally, define the texture and the texture environment    */
  146.  
  147.     texdef2d (tex_id, 4, width, height, teximage, 0, NULL);
  148.  
  149.     free (buf);
  150.     free (teximage);
  151.     iclose (ifile);
  152.     free (namebuf);
  153.  
  154.     return (TRUE);
  155.     }
  156.